package de.wwi11b2.mjp.comicvineclient;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;

public class SearchActivity extends Activity {

	private EditText searchEditText = null;
	private RadioGroup resourceTypeRadioGroup = null;
	private Button goBotton = null;
	private Button clearBotton = null;
	private Item item = null;
	private boolean error = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search);
		searchEditText = (EditText) findViewById(R.id.searchEditText);
		resourceTypeRadioGroup = (RadioGroup) findViewById(R.id.resourceTypRadioGroup);
		goBotton = (Button) findViewById(R.id.searchGoButton);
		goBotton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				SearchTask searchTask = new SearchTask();
				String[] args = { searchEditText.getText().toString() };
				searchTask.execute(args);
			}
		});
		clearBotton = (Button) findViewById(R.id.clearButton);
		clearBotton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				searchEditText.setText("");
			}
		});
		findViewById(R.id.comicVineLinkTextView2).setOnClickListener(
				new ComicVineOnClickListener(getApplicationContext()));

	}

	private class SearchTask extends AsyncTask<String, Integer, Item> {

		private ProgressDialog dialog;

		public SearchTask() {
			dialog = new ProgressDialog(SearchActivity.this);
		}

		@Override
		protected void onPreExecute() {
			// set up the dialog
			dialog.setTitle(R.string.title_activity_search);
			dialog.setIndeterminate(true);
			dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			dialog.setMessage(getString(R.string.waiting_for_comic_vine));
			dialog.setCancelable(false);
			dialog.show();
		}

		@Override
		protected Item doInBackground(String... arg0) {
			try {
				String searchText = arg0[0];
				Item.ResourceType type = null;
				int selection = resourceTypeRadioGroup
						.getCheckedRadioButtonId();
				switch (selection) {
				case R.id.characterRadio:
					type = Item.ResourceType.CHARACTER;
					break;
				case R.id.locationRadio:
					type = Item.ResourceType.LOCATION;
					break;
				default:
					Toast.makeText(getApplicationContext(), "Choose a type!",
							Toast.LENGTH_LONG).show();
					return null;
				}
				item = DataHelper.search(searchText, type);

			} catch (Exception e) {
				error = true;
				e.printStackTrace();
			}

			return item;
		}

		@Override
		protected void onPostExecute(Item result) {
			dialog.dismiss();
			if (error) {
				Toast.makeText(getApplicationContext(),
						"Oops! Something went terribly wrong!",
						Toast.LENGTH_LONG).show();
			} else {
				Intent intent = new Intent();
				intent.setClass(getApplicationContext(), ItemActivity.class);
				intent.putExtra("item", item);
				intent.putExtra("isFromSearch", true);
				startActivity(intent);
			}
			item = null;
			error = false;
		}
	}

}
